import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    allchecked: false,
    isEdit: false,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    goodsList: [],
    totalPrice: 0,
    cartList: [] 
  },

  onShow: function() {
    this.geilist()
    if (wx.getStorageSync('userInfo')){
      this.getGoodsList()
    }
    
  },

  //编辑
  handleEdit: function() {
    this.setData({
      isEdit: !this.data.isEdit
    })
  },

  //删除
  handleDel: function(e) {
    var id = e.target.dataset.id;
    var that = this
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: '确定删除该商品',
      success: function(res) {
        if (res.confirm) {
          that.delRequest(id)
        }
      }
    })

  },

  delRequest: function(id) {
    var goodslist = this.data.cartList;
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/shoppingCart/delete?carId=' + id,
      method: 'POST',
      data: {}
    }, (res) => {
      var newlist = goodslist.filter((item, index) => item.id != id)
      that.setData({
        cartList: newlist
      })
      //数据小于0取消全选
      if (newlist.length < 1) {
        that.setData({
          allchecked: false
        })
      }
      that.hendleTotalPrice();
      wx.showToast({
        title: '删除成功',
      })
    })
  },

  // 全选
  onChange: function(e) {
    var goodslist = this.data.cartList
    goodslist.map((item, index) => {
      item.checked = !this.data.allchecked
    })
    this.setData({
      cartList: goodslist,
      allchecked: !this.data.allchecked
    })
    this.hendleTotalPrice()
  },


  //单选
  changeitem: function(e) {
    var key = e.target.dataset.key;
    var goodslist = this.data.cartList;
    var flag = false;
    var checkNum = 0
    goodslist.map((item, index) => {
      if (index === key) {
        item.checked = !item.checked
      }
      if (item.checked === true) {
        checkNum++
      }
    })
    //判断选中个数是否与订单列表长度相等，
    if (checkNum == goodslist.length) {
      flag = true
    }
    this.setData({
      cartList: goodslist,
      allchecked: flag
    })
    this.hendleTotalPrice()
  },


  //监听购物车数量
  handleCart(e) {
    var num = e.detail
    var key = e.target.dataset.key;
    var goodslist = this.data.cartList
    var gdsItem = ''
    goodslist.map((item, index) => {
      if (index === key) {
          item.qty = num,
          gdsItem = {
            id: Number(item.id),
            gdsId: Number(item.gdsId),
            qty: item.qty
          }
      }
    })
    this.setData({
      cartList: goodslist
    }) 
    this.addCate(gdsItem)
    this.hendleTotalPrice()
  },


  hendleTotalPrice() { //计算总价
    let that = this
    let price = 0
    for (let i = 0; i < that.data.cartList.length; i++) {
      if (that.data.cartList[i].checked) {
        price = price + that.data.cartList[i].price * that.data.cartList[i].qty
      }
    }
    price = price.toFixed(2)
    this.setData({
      totalPrice: price
    })
  },

  //获取购车商品列表
  geilist: function() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/shoppingCart/list',
      method: 'POST',
      data: {}
    }, (res) => {
      var list = res.list;

      // list.map(item=>{
      //   item.checked=true
      // })

      this.setData({
        cartList: list
      })
    })
  },
// /odr/protected/comm/beanCrud/mall.ShoppingCartVO/update

  //加入购物车
  addCate: function(params) { 
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/comm/beanCrud/mall.ShoppingCartVO/update',
      method: 'post',
      data: params
    }, (res) => {
      // wx.showToast({
      //   title: '加入购物车成功',
      // })
    })
  },

  //提交订单
  onClickButton: function() {
    var cartList = this.data.cartList;
    var goodsList = []
    cartList.map(item => {
      if (item.checked) {
        goodsList.push(item)
      }
    })

    if (goodsList.length > 0) {
      wx.setStorageSync('goodsOdrList', goodsList)
      wx.setStorageSync('odrTotalPrice', this.data.totalPrice)
      wx.navigateTo({
        url: '/pages/settle/index',
      })
    } else {
      wx.showToast({
        title: '请选择商品',
      })
    }
  },

  //商品列表
  getGoodsList: function () {
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/gds/goods/recommend',
      method: 'POST',
      data:{
        cusId: wx.getStorageSync('userInfo').id,
        pageNo:1,
        pageSize:9
      }
    }, (res) => {
      let goodsList = res.list;
      this.setData({
        goodsList
      });
    })
  },

  //跳转详情页
  toGoodsDetail(e) {
    var id = e.currentTarget.dataset.id
    console.log(e)
    wx.navigateTo({
      url: '/pages/good/detail/index?id=' + id,
    })
  },
})